//
//  LCServiceViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/4.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCServiceViewController.h"
#import "LCDetailsViewController.h"
#import "LCHeadView.h"
#import "LCServiceCell.h"
#import "ListModel.h"
static NSString *cellIdentifier = @"LCServiceCell";

@interface LCServiceViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _page;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableDictionary  *dataDic;
@property (strong,nonatomic)NSMutableArray       *dataArray;

@end

@implementation LCServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"政策服务"];
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"LCServiceCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{//下拉刷新
        _page = 1;
        [self loadData];
    }];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{//上拉刷新
        [self loadData];
    }];
    [_tableView.mj_header beginRefreshing];
    _tableView.mj_footer.hidden = YES;
}
-(void)loadData
{
    if (_page == 1)
    {
        [_dataArray removeAllObjects];
    }
    [_dataDic setObject:@"2" forKey:@"column_id"];
    [_dataDic setObject:@"10" forKey:@"pageSize"];
    [_dataDic setObject:[NSString stringWithFormat:@"%ld",_page] forKey:@"pageNum"];
    [MDataProvider getPhotoListBodyDic:_dataDic sucess:^(NSArray *array)
    {
        [_dataArray addObjectsFromArray:array];
        _page++;
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        if (array.count<10)
        {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else
        {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
    } faile:^(NSString *str) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LCHeadView *view = [[LCHeadView alloc]initWithTitle:@"政策服务"];
    return view;
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListModel *model = [_dataArray objectAtIndex:indexPath.row];
    LCServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.stateLbl.hidden = YES;
    cell.titleLbl.text = model.title;
    cell.contentLbl.text = model.summary;
    cell.timeLbl.text = model.published_date;
     NSString *imgUrl = [NSString stringWithFormat:@"%@/%@%@",ENCLOUD_URL,model.file_path,model.file_name];
    [cell.iconImg sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListModel *model = [_dataArray objectAtIndex:indexPath.row];
    LCDetailsViewController *vc = [[LCDetailsViewController alloc]init];
    vc.info_id = model.info_id;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
